<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use app\common\model\Activity as activityModel;
use app\common\model\ActivitySignup;
use app\common\model\Type;

class Activity extends Admin {

    public $activity;
    public $signup;

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
        $this->activity = new activityModel();
        $this->signup = new ActivitySignup();
        $typeModel = new Type();
        $this->assign('activityType', config('activity_type'));
        $this->assign('activityClass', $typeModel->getValueByPid(7));
        $this->assign('activityStatus', config('default_status'));
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index() {
        $list = $this->activity->order('list_order')->paginate(config('paginate.list_rows'));
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 编辑/添加活动.
     *
     * @return \think\Response
     */
    public function editActivity() {
        $param = $this->request->param();
        if (isset($param['startdate']) && $param['startdate']) {
            $param['startdate'] = strtotime($param['startdate']);
        }
        if (isset($param['enddate']) && $param['enddate']) {
            $param['enddate'] = strtotime($param['enddate']);
        }
        if ($param['id']) {
            $this->activity->where('id', $param['id'])->update($param);
        } else {
            $param['addtime'] = time();
            $this->activity->insert($param);
        }
        $this->success('操作成功');
    }

    /**
     * 删除活动
     * @param type $id
     */
    public function delActivity($id) {
        $this->activity->where('id', $id)->delete();
        $this->signup->where('activityid', $id)->delete();
        $this->success('删除成功');
    }

    /**
     * 更新活动排序
     */
    public function setOrder() {
        $param = $this->request->param();

        $this->_setorder($param['list_order'], 'activity');

        $this->success('排序成功');
    }

    /**
     * 活动报名列表
     * @param type $id
     */
    public function signup($id) {

        $activity = $this->activity->find($id);
        $this->assign('activity', $activity);

        $list = $this->activity->order('list_order')->paginate(config('paginate.list_rows'));
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id) {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id) {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id) {
        //
    }

}
